package com.feicheng.controller.system;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.CnRegionInfo;
import com.feicheng.persistence.service.ICnRegionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2024-08-21
 */
@RestController
@RequestMapping("/admin/region")
public class CnRegionInfoController {

    @Autowired
    ICnRegionInfoService cnRegionInfoService;

    @GetMapping("/info")
    ResultInfo getRegionByCodeAndScode(String code, String scode) {
        return cnRegionInfoService.getRegionByCodeAndScode(code, scode);
    }

    @GetMapping("/all")
    ResultInfo getAll(String scode) {
        QueryWrapper<CnRegionInfo> qw = new QueryWrapper<>();
        List<CnRegionInfo> list = cnRegionInfoService.list(qw);

        JSONObject result = new JSONObject();
        JSONObject nameMap = new JSONObject();
        for(Iterator<CnRegionInfo> it = list.iterator(); it.hasNext();) {
            CnRegionInfo info = it.next();
            nameMap.put(info.getCriname(), info);
        }
        result.put("nameMap", nameMap);

        return ResultInfo.success(result);
    }
}
